import { _decorator, Color, Component, instantiate, Label, Node, Prefab, Sprite, v3 } from 'cc';
import { Tech, TechType } from '../entity/Tech';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { Utils } from '../common/Utils';
import StudyApi from '../api/StudyApi';
import { UserInfo } from '../common/UserInfo';
import { ConItem } from './ConItem';
import { TrainProperty } from '../train/TrainProperty';
import { Soldier } from '../entity/Soldier';
const { ccclass, property } = _decorator;

@ccclass('DevelopTech')
export class DevelopTech extends Component {
    @property(Node)
    bg:Node
    @property(Node)
    close:Node
    @property(Label)
    title:Label
    @property(Sprite)
    cover:Sprite
    @property(Sprite)
    border:Sprite
    @property(Label)
    soldierName:Label
    @property(Node)
    conList:Node
    @property(Label)
    desc:Label
    @property(Label)
    food:Label
    @property(Label)
    wood:Label
    @property(Label)
    mud:Label
    @property(Label)
    iron:Label
    @property(Label)
    time:Label
    @property(Node)
    btnPos:Node
    @property(Node)
    cancel:Node
    @property(Node)
    develop:Node
    @property(Node)
    canNot:Node
    @property(Label)
    canNotLabel:Label
    @property(Node)
    lookPro:Node



    @property(Prefab)
    conItem:Prefab

    techData:Tech

    red:Color =  new Color("#BF0E0E")
    green:Color = new Color("#57840F")

    start() {
        this.close.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.cancel.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.bg.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.develop.on(Node.EventType.TOUCH_END, this.onDevelop, this)
        this.lookPro.on(Node.EventType.TOUCH_END, this.onLookPro, this)
    }

    async setData(tech:Tech){
        this.techData = tech
        this.title.string = tech.name + '研究'

        this.cover.spriteFrame = await tech.getTechImg()
        this.soldierName.string = tech.name
        
        this.setCondition(tech)

        this.desc.string = tech.desc

        this.food.string = tech.studyRes.food + ''
        if(!tech.isFoodEnough){
            this.food.color = this.red
        }

        this.mud.string = tech.studyRes.mud + ''
        if(!tech.isMudEnough){
            this.mud.color = this.red
        }

        this.wood.string = tech.studyRes.wood + ''
        if(!tech.isWoodEnough){
            this.wood.color = this.red
        }

        this.iron.string = tech.studyRes.iron + ''
        if(!tech.isIronEnough){
            this.iron.color = this.red
        }

        this.time.string = Utils.secondToStr(tech.studyTime)  + ''

       

        this.showBtns()
    }

    showBtns(){
         // 判断是否有其他的科技正在研究
         let eventList = Tech.getTechStudyEventList()
         if(eventList.length > 0){
             this.canNotLabel.string = '正在进行其他研究'
             this.canNot.position = this.btnPos.position
             return
         }



        if(!this.techData.isBuildingEnough){
            for(let c of this.techData.condition){
               if(!c.isEnough){
                this.canNotLabel.string = '需要升级' + c.name
               }
            }
            
        }
        if(!this.techData.isResEnough){
            this.canNotLabel.string = '资源不足'
        }

        if(!this.techData.isBuildingEnough || !this.techData.isResEnough){
            if(this.techData.type == TechType.Soldier){
                this.lookPro.position = v3(-150, this.btnPos.position.y, 0)
                this.canNot.position = v3(150, this.btnPos.position.y, 0)
            } else {
                this.canNot.position = this.btnPos.position
            }
            return
        }

       

        if(this.techData.type == TechType.Soldier){
            this.lookPro.position = v3(-150, this.btnPos.position.y, 0)
            this.develop.position = v3(150, this.btnPos.position.y, 0)
        } else {
            this.develop.position = this.btnPos.position
        }

      
    }

    setCondition(tech:Tech){
        this.conList.destroyAllChildren()
        for(let c of tech.condition){
            let node = instantiate(this.conItem)
            node.getComponent(ConItem).setData(c)
            this.conList.addChild(node)
        }
    }

    onClose(){
        UIManager.instance.closeUI(PrefabEnum.DevelopTech)
    }

    async onDevelop(){
        await UIManager.instance.showLoading()
        let res = await StudyApi.studying({
            a: this.techData.id
        })
        UIManager.instance.closeLoading()
        this.onClose()
        UserInfo.instance.refreshVillageDetail()
    }

    async onLookPro(){
        let ui = await UIManager.instance.openUI(PrefabEnum.TrainProperty)
        let so = Soldier.createById(this.techData.id)
        
        ui.getComponent(TrainProperty).setData(so)
    }
    
}


